﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Plugin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vant.CellTypes;

namespace Vant.WpfControls.DrawingObject
{
    /// <summary>
    /// Interaction logic for GridDrawingObject.xaml
    /// </summary>
    public partial class GridDrawingObject : UserControl
    {
        public GridDrawingObject(CellTypes.Grid grid, CellTypeViewModelStyle cellStyle, ICellInfo cell, IDrawingHelper drawingHelper)
        {
            var viewModel = new GridDrawingObjectViewModel();
            cellStyle.Foreground = cellStyle.Foreground ?? drawingHelper.GetBrush("Text 1 25");
            cellStyle.TextWrapping = TextWrapping.Wrap;
            List<GridItem> items = new List<GridItem>();
            if (grid.useBinding)
            {
                for (int i = 0; i < grid.columnNum * 2; i++)
                {
                    items.Add(new GridItem()
                    {
                        text = Properties.Resources.Grid_GridItem_Text + (i + 1),
                        icon = new ImageValue()
                        {
                            Name = "Navigation/175_image_3.svg",
                            BuiltIn = true,
                            Color = "Text 1 25",
                        }
                    });
                }
            }
            else
            {
                foreach (var item in grid.options)
                {
                    if (item is GridItem gridItem)
                    {
                        items.Add(gridItem);
                    }
                }
            }

            var colCount = grid.columnNum;
            var rowCount = Math.Min(1, (int)Math.Ceiling((double)items.Count / grid.columnNum));
            viewModel.Margin = new Thickness(grid.gutter, 0, 0, 0);
            for (int i = 0; i < (colCount * rowCount) - items.Count; i++)
            {
                items.Add(new GridItem());
            }

            for (int i = 0; i < items.Count; i++)
            {
                var itemViewMode = new GridItemViewModel();
                var item = items[i];
                itemViewMode.Margin = new Thickness(0, 0, grid.gutter, grid.gutter);
                var borderWidth = 0;
                if (grid.border)
                {
                    borderWidth = colCount;
                }
                itemViewMode.Width = (drawingHelper.GetImageCellSize().Width - grid.gutter * (colCount + 1) - borderWidth - 1) / colCount;
                if (grid.square)
                {
                    itemViewMode.Height = itemViewMode.Width;
                }
                if (grid.border)
                {
                    itemViewMode.BorderThickness = new Thickness(1);
                }
                if (grid.direction == "horizontal")
                {
                    itemViewMode.Orientation = Orientation.Horizontal;
                    itemViewMode.TextMargin = grid.reverse ? new Thickness(0, 0, 8, 0) : new Thickness(8, 0, 0, 0);
                }
                else
                {
                    itemViewMode.Orientation = Orientation.Vertical;
                    itemViewMode.TextMargin = grid.reverse ? new Thickness(0, 0, 0, 8) : new Thickness(0, 8, 0, 0);
                }
                if (grid.center)
                {
                    itemViewMode.HorizontalAlignment = HorizontalAlignment.Center;
                    itemViewMode.VerticalAlignment = VerticalAlignment.Center;
                }
                if (grid.square && grid.center)
                {
                    itemViewMode.BorderPadding = new Thickness(8, 0, 8, 0);
                }

                itemViewMode.IconSize = grid.iconSize;
                itemViewMode.Icon = IconHelper.GetIcon(item.icon, drawingHelper);
                itemViewMode.Text = item.text;
                itemViewMode.CellStyle = cellStyle;
                itemViewMode.Reverse = grid.reverse;
                viewModel.Items.Add(itemViewMode);
            }

            this.DataContext = viewModel;
            InitializeComponent();
        }

        public class GridDrawingObjectViewModel : ModelBase
        {
            public List<GridItemViewModel> Items { get; set; }=new List<GridItemViewModel>();
            public Thickness Margin { get; set; }
        }

        public class GridItemViewModel
        {
            public Thickness Margin { get; set; }
            public Thickness TextMargin { get; set; }
            public Thickness BorderPadding { get; set; } = new Thickness(8, 16, 8, 16);
            public Thickness BorderThickness { get; set; } = new Thickness(0);

            public HorizontalAlignment HorizontalAlignment { get; set; } = HorizontalAlignment.Left;
            public VerticalAlignment VerticalAlignment { get; set; } = VerticalAlignment.Top;

            public Orientation Orientation { get; set; }
            public object Icon { get; set; }
            public string Text { get; set; }
            public double Width { get; set; } = double.NaN;
            public double Height { get; set; } = double.NaN;

            public double IconSize { get; set; }
            public CellTypeViewModelStyle CellStyle { get; set; }

            public bool Reverse { get; set; }
            public Visibility ReverseTextVisiblity
            {
                get
                {
                    return Reverse ? Visibility.Visible : Visibility.Collapsed;
                }
            }
            public Visibility NormalTextVisiblity
            {
                get
                {
                    return !Reverse ? Visibility.Visible : Visibility.Collapsed;
                }
            }

        }
    }
}
